﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using System.Data;
using Maticsoft.Common;


namespace WEB.workPlan
{
    public partial class workPlanView : System.Web.UI.Page
    {
        T_workPlan bll = new T_workPlan();
        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            //加载时间
            if (!IsPostBack)
            {
                tbTimestart.Text = DateTime.Now.AddMonths(-1).ToShortDateString();
                tbTimestop.Text = DateTime.Now.ToString("yyyy/MM/dd");
            }

            //搜索的条件
            string strSql = Query();
            if (!strSql.Trim().Equals("1=1"))
            {
                ds = bll.GetList(strSql);
                gvPlan.DataSource = ds;
            }
            else
            {
                ds = bll.GetList("");
                gvPlan.DataSource = ds;
            }


            if (!IsPostBack)
            {
                this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");//日期控件
                this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");//日期控件
                ds = bll.GetList(strSql);
                gvPlan.DataSource = ds;
                gvPlan.DataBind();
            }
        }

        /// <summary>
        /// 日期判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btQuery_Click(object sender, EventArgs e)
        {
            if (tbTimestart.Text != "" && tbTimestop.Text != "")
            {
                if (Convert.ToDateTime(tbTimestart.Text) > Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能大于开始日期！");
                    return;
                }
                else if (Convert.ToDateTime(tbTimestart.Text) == Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能等于开始日期！");
                }
            }
            ds = bll.GetList(Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();
        }

        /// <summary>
        /// DropDownList控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPlan_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                DropDownList ddl = e.Row.Cells[12].FindControl("ddlstates") as DropDownList;//获得ddl控件
                if (ds != null)
                {
                    string value = ds.Tables[0].Rows[e.Row.RowIndex]["states"].ToString();//取值
                    if (value != "0")//作废状态 取消作废按钮才可用
                    {
                        Button bt = (Button)e.Row.Cells[4].FindControl("Button2");
                        bt.Enabled = false;
                    }
                    if (value != "1")//未完成状态 修改按钮才可用
                    {
                        Button bt2 = (Button)e.Row.Cells[1].Controls[0];
                        bt2.Enabled = false;
                        
                    }
                    if (value != "1")//未完成状态 作废按钮才可用
                    {
                        Button bt3 = (Button)e.Row.Cells[3].FindControl("Button3");
                        bt3.Enabled = false;

                    }
                    if (value == "2")//完成状态 删除按钮不可用
                    {
                        Button bt4 = (Button)e.Row.Cells[2].FindControl("Button1");
                        bt4.Enabled = false ;
                    }
                    
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
                    ddl.Enabled = false;
                }
            }
        }
        /// <summary>
        /// ButtonFiled按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPlan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();

           if (e.CommandName == "Alter")//修改
            {
               int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
               Response.Redirect("workPlanEdit.aspx?ID=" + id);//页面跳转
            }

           if (e.CommandName == "go")
           {
               
                   TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
                   int num = Int32.Parse(tb.Text);
                   GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                   gvPlan_PageIndexChanging(null, ea);
           

           }
        }

        /// <summary>
        /// 搜索方法
        /// </summary>
        /// <returns>搜索语句</returns>
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;

            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 1:
                    case 0:
                    case 2:
                        strSql += string.Format("and {0} like '%{1}%' ", strQuery, tbQuery.Text.Trim());
                        break;
                    default: 
                        if (!PageValidate.IsNumber(tbQuery.Text.Trim())) 
                            tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                }
            }
            if (tbTimestart.Text != "")
            {
                DateTime timeStart = Convert.ToDateTime(tbTimestart.Text.Trim());
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and planDate between '" + timeStart + "' and '" + timeStop + "'";
                }
                else
                {
                    DateTime timeStop = Convert.ToDateTime(tbTimestop.Text.Trim());
                    strSql = strSql + " and planDate between '" + timeStart + "' and '" + timeStop + "'";
                }
            }
            if (ddl.SelectedValue != "3")//全部状态
            {
                strSql += string.Format("and states = '{0}'", ddl.SelectedItem.Value);
            }
            strSql += " order by planDate desc";
            return strSql;
        }
        /// <summary>
        /// 状态的筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            ds = bll.GetList(Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();


        }
        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            if (bll.Delete(id))
            {
                MessageBox.ShowAndRedirect(this, "删除成功", "./workPlanView.aspx");
            }
            else
            {
                MessageBox.Show(this, "删除失败");
            }
        }

      /// <summary>
      /// 取消作废按钮
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        protected void BtnRevive_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            CustomsSystem.Model.T_workPlan model = bll.GetModel(id);
            model.states = 1;
            bll.Update(model);
            if (bll.Update(model))
            {

                MessageBox.ShowAndRedirect(this, "取消作废成功", "./workPlanView.aspx");
            }
            else
            {
                MessageBox.Show(this, "取消作废失败");
            }
        }

        /// <summary>
        /// 作废按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnInvalid_Click(object sender, EventArgs e)
        {

            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            CustomsSystem.Model.T_workPlan model = bll.GetModel(id);
            model.states = 0;
            bll.Update(model);
            if (bll.Update(model))
            {

                MessageBox.ShowAndRedirect(this, "作废成功", "./workPlanView.aspx");
            }
            else
            {
                MessageBox.Show(this, "作废失败");
            }
        }

        protected void gvPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
                gvPlan.PageIndex = e.NewPageIndex;//绑定最新页
                gvPlan.DataBind();
                TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (gvPlan.PageIndex + 1).ToString();

        }

        //protected void gvPlan_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    int id = Convert.ToInt32(gvPlan.DataKeys[e.NewEditIndex].Value);//获取ID
        //    Response.Redirect("workPlanEdit.aspx?ID=" + id);//页面跳转
        //}

    }


}